package com.sgb.kjwl.view.ui.activity.self.userInfo;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityChangeMobileBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.view.widget.SimpleCountDownTimer;
import com.sgb.kjwl.viewmodel.self.userInfo.ChangeMobileModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;

import javax.inject.Inject;

public class ChangeMobileActivity extends AppActivity {
    private ActivityChangeMobileBinding binding;
    @Inject
    ChangeMobileModel viewModel;

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(ChangeMobileActivity.this, R.layout.activity_change_mobile);
        binding.setViewModel(viewModel);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void init() {
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("修改手机号码");
        binding.tvCheckCodeButton.setOnClickListener(this);
        binding.tvSave.setOnClickListener(this);
    }

    private void codeValidation() {
        if (TextUtils.isEmpty(binding.etMobile.getText())) {
            MToast.showToast(ChangeMobileActivity.this, "手机号码不能为空");
            return;
        }
        if (!CheckUtils.isMobileNum(binding.etMobile.getText().toString())) {
            MToast.showToast(ChangeMobileActivity.this, "请输入正确手机格式");
            return;
        }
        SimpleCountDownTimer mCountDownTimerUtils = new SimpleCountDownTimer(this,binding.tvCheckCodeButton, 60000, 1000); //倒计时1分钟
        mCountDownTimerUtils.start();
        viewModel.getCodeModel(binding.etMobile.getText().toString());
    }

    private void saveValidation() {
        if (TextUtils.isEmpty(binding.etMobile.getText())) {
            MToast.showToast(ChangeMobileActivity.this, "手机号码不能为空");
            return;
        }
        if (TextUtils.isEmpty(binding.etCode.getText())) {
            MToast.showToast(ChangeMobileActivity.this, "验证码不能为空");
            return;
        }
        if (TextUtils.isEmpty(binding.etPwd.getText())) {
            MToast.showToast(ChangeMobileActivity.this, "密码不能为空");
            return;
        }
        if (!CheckUtils.isMobileNum(binding.etMobile.getText().toString())) {
            MToast.showToast(ChangeMobileActivity.this, "请输入正确手机格式");
            return;
        }
        if (binding.etCode.getText().toString().length() != 6) {
            MToast.showToast(ChangeMobileActivity.this, "验证码长度为6位数");
            return;
        }
        if (binding.etPwd.getText().toString().length() < 6 || binding.etPwd.getText().toString().length() > 18) {
            MToast.showToast(ChangeMobileActivity.this, "密码长度为6-18位");
            return;
        }
        viewModel.saveModel(binding.etMobile.getText().toString(), binding.etCode.getText().toString(), binding.etPwd.getText().toString());
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_check_code_button:
                codeValidation();
                break;
            case R.id.tv_save:
                saveValidation();
                break;
        }

    }
}
